package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONObject;

public class Commodity_message extends AppCompatActivity {
    private String findby_id;
    public static final String EXTRA_ID="ID";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_message);
        int id=getIntent().getExtras().getInt(EXTRA_ID);
        findby_id(id);
        try {

            JSONArray jsonArray= new JSONArray(findby_id);
            String name = null;
            String description=null;
            String price=null;
            String image=null;
            for (int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                name=jsonObject.getString("name");
                description=jsonObject.getString("description");
                price=jsonObject.getString("price");
                image=jsonObject.getString("image");

            }
            TextView textView =findViewById(R.id.name);
            textView.setText(name);

            TextView textView1 =findViewById(R.id.description);
            textView1.setText(description);

            TextView textView3 =findViewById(R.id.price);
            textView3.setText(price);

            Context ctx=getBaseContext();
            int resId = getResources().getIdentifier(image, "drawable" ,ctx.getPackageName());

            ImageView photo = findViewById(R.id.image);
            photo.setImageResource(resId);


        }catch (Exception e)
        {
            e.printStackTrace();
        }



    }

    public void findby_id(int id)
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    findby_id=request.sendGet("http://169.254.132.31:8081/Commdity/findby_id","id="+id);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}